package com.dealdata.service;

import com.base.bean.ClientPhone;
import com.base.service.client.AdvanceClientService;
import java.util.*;

import org.hibernate.Session;
import org.hibernate.Transaction;

import tool.ReadTool;
import tool.WriteTool;

public class DataDeal {

	private WriteTool writer;
	private AdvanceClientService as;

	/**
	 * 第一批导的客户有问题 电话发生了重复映射到两个不同的客户上去了
	 * 
	 * 先将crm导入的客户记录全部删除 重新导入合乎要求的客户
	 * 
	 * 同cid两个电话 下面为真（id较大的哪个） 上面为假
	 * 
	 * select a.cid,a.created ,b.* from (select * from client_phone where
	 * phone='13982636609') a left join client_base b on a.cid=b.cid
	 * 
	 * 
	 * 将导入的crm客户 有重复的重复电话标记出来 并作废
	 * 
	 * select b.* from (select * from client_base where name='赵维宝')a left join
	 * client_phone b on a.cid=b.cid
	 * 
	 * delete from client_phone where phone='13991409311' and id<>72577
	 * 
	 * 
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		DataDeal work = new DataDeal();
		// String filePath="/Users/chz/Desktop/test";
		//
		// for(Integer i=0;i<1000;i++){
		// work.task_1(filePath+".txt",i*100,100); //找到重复的电话号码（cid id{phone}
		// number crmFlag created）
		// System.out.println(i+"---1000");
		// }

		// work.task_2("/Users/chz/Desktop/test.txt",
		// "/Users/chz/Desktop/test_2.txt");
		// work.task_3("/Users/chz/Desktop/test_2.txt",
		// "/Users/chz/Desktop/test_3.txt");
		// work.task_4("/Users/chz/Desktop/test_3.txt","/Users/chz/Desktop/test_4.txt");
//		work.task_5("/Users/chz/Desktop/test_3.txt");

	}

	// 生成1|13550698051|97610 （count|phone|id）
	public void task_1(String filePath, int offset, int pageSize) {
		as = AdvanceClientService.getInstance();
		writer = WriteTool.getInstance(filePath);
		List<ClientPhone> cps = as.getPhoneIdByPage(offset, pageSize);
		for (ClientPhone cp : cps) {
			Integer i = as.checkPhone(cp.getPhone());
			writer.write(i.toString() + "|" + cp.getPhone() + "|" + cp.getId());
		}

		writer.close();

	}

	// 去掉为count为1的记录
	public void task_2(String input, String output) {
		WriteTool writer = WriteTool.getInstance(output);

		List<String> list = ReadTool.writeToDat(input);

		String regex = "^1\\|.*";
		for (String s : list) {
			if (s.matches(regex))
				continue;
			else {
				writer.write(s);
			}

		}
		writer.close();

	}

	class Phone {
		String id;
		String phone;
		String count;

		public Phone(String count, String phone, String id) {
			this.id = id;
			this.phone = phone;
			this.count = count;
		}
	}

	// 分离靠前的重复phone
	public void task_3(String input, String output) {
		List<String> list = ReadTool.writeToDat(input);
		Map<String, Phone> map = new HashMap<String, Phone>();
		for (String s : list) {
			String[] arr = s.split("\\|");

			map.put(arr[1], new Phone(arr[0], arr[1], arr[2]));
		}
		WriteTool writer = WriteTool.getInstance(output);

		for (Map.Entry<String, Phone> entry : map.entrySet()) {

			// System.out.println("Key = " + entry.getKey() + ", Value = " +
			// entry.getValue());
			Phone p = (Phone) entry.getValue();
			writer.write(p.count + "|" + p.phone + "|" + p.id);

		}
		writer.close();

	}

	// 生成delete批处理语句
	public void task_4(String input, String output) {
		List<String> list = ReadTool.writeToDat(input);
		List<String> outList = new ArrayList<String>();
		WriteTool writer = WriteTool.getInstance(output);

		for (String s : list) {
			String[] arr = s.split("\\|");
			String r = "delete from ClientPhone cp  where cp.phone='" + arr[1]
					+ "' and cp.id<>" + arr[2];
			writer.write(r);
		}
		writer.close();
	}

	// 处理一条delete
	private void delete(String phone, Integer notId) {
		
		Session session = null;
		Transaction tx = null;
		try {
			session = AdvanceClientService.getInstance().getCpd()
					.getSessionFactory().getCurrentSession();
			tx = session.beginTransaction();

			String hqlDelete = "delete  ClientPhone  cp  where cp.phone= :phone and cp.id<> :id ";
			// or String hqlDelete = "delete Customer where name = :oldName";
			int deletedEntities = session.createQuery(hqlDelete)
					.setString("phone", phone).setInteger("id", notId)
					.executeUpdate();

			tx.commit();

		} finally {
			if (session.isOpen())
				session.close();
		}
	}

	// 生成delete批处理语句
	public void task_5(String input) {
		List<String> list = ReadTool.writeToDat(input);
		int i=0;
		int j=0;
		for (String s : list) {
			i=i+1;
			String[] arr = s.split("\\|");
			this.delete(arr[1], new Integer(arr[2]));
			if(i>100){
				i=0;
				j=j+1;
				System.out.println("do numbers:"+j*100);
			}

		}
		

	}

}
